package com.chaodows.commons.util.qrcode;

import com.chaodows.commons.util.QRCodeUtil;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.springframework.util.ClassUtils;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;

/**
 * ============================
 *
 * @Author pengzhichao
 * @Description //TUDO $
 * @Date 2021/8/4 15:00
 * @Version 1.0
 * ============================
 **/
public class QRCodeGenerator {

    private static final String QR_CODE_IMAGE_PATH = "E:\\myphoto.png";

    private static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException, WriterException {

        QRCodeWriter qrCodeWriter = new QRCodeWriter();

        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);

        Path path = FileSystems.getDefault().getPath(filePath);

        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);

    }

    public static void main(String[] args) {
        /*try {
            generateQRCodeImage("https://www.baidu.com", 350, 350, QR_CODE_IMAGE_PATH);
        } catch (WriterException e) {
            System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("Could not generate QR Code, IOException :: " + e.getMessage());
        }*/
        String text = "https://www.baidu.com";
        String impagePath = "C:/Users/BDB032017006B/Pictures/Camera Roll/约翰逊_1.jpg";
        String destPath = "E:/other.jpg";
        try {
            QRCodeUtil.encode(text,null,destPath,true);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
